grafica <- function(f,i,d,t,color="red"){
  #f_a <- function(x){sqrt(x)-cos(x)}

  x <- seq(i, d, by=0.0001)
  y <- f(x)
  
  graf_a <- ggplot()+
    geom_vline(xintercept = 0, linetype="dashed")+ #eje x
    geom_hline(yintercept = 0, linetype="dashed")+ #eje y
    geom_line(aes(x=x, y=y), color=color, linewidth=1)+
    #coord_fixed(ratio = 1)+ # misma escala en los ejes
    labs(x="x", y="f(x)", title=t)+
    theme_bw()
  
  
  ggplotly(graf_a)
}
graficas <- function(g,i,d,t){
f <- function(x){x}
#g <- function(x){(2-exp(x)+x^2)/3}
h <- function(x){g(x)-f(x)}

graf <- ggplot()+
  #geom_vline(xintercept = 0, linetype="dashed")+ #eje x
  #geom_hline(yintercept = 0, linetype="dashed")+ #eje y
  #geom_line(aes(x=x_a, y=y_a), color="red", linewidth=1)+
  geom_function(fun=f,color="red",linewidth=0.75,n=200)+
  geom_function(fun=g,color="blue",linewidth=0.75,n=200)+
  geom_function(fun=h,color="yellow",linewidth=0.75,n=200)+
  #coord_fixed(ratio = 1)+ # misma escala en los ejes
    xlim(i,d)+
  labs(x="x", y="f(x)", title=t)+
  theme_bw()


ggplotly(graf)
}

Ejercicio 1

Sea \(f(x)=\sqrt{x}-\cos x\). Usa el método de la bisección para encontrar \(x\in [0,1]\) tal que \(f(x)=0\).

f <- function(x){sqrt(x)-cos(x)}
grafica(f,0,1.5,"Primera gráfica")

Otra opción

graf_a <- ggplot()+
  #geom_vline(xintercept = 0, linetype="dashed")+ #eje x
  #geom_hline(yintercept = 0, linetype="dashed")+ #eje y
  geom_function(fun=f, color="red", linewidth=1, n=200)+
  #coord_fixed(ratio = 1)+ # misma escala en los ejes
  xlim(0, 1.5)+
  labs(x="x", y="f(x)", title="Primera gráfica")+
  theme_bw()


ggplotly(graf_a)

Utilizando la función desarrollada en clase

metodo_biseccion(f,0,1,1e-10,N=500)
## $aprox
##  [1] 0.5000000 0.7500000 0.6250000 0.6875000 0.6562500 0.6406250 0.6484375
##  [8] 0.6445312 0.6425781 0.6416016 0.6420898 0.6418457 0.6417236 0.6416626
## [15] 0.6416931 0.6417084 0.6417160 0.6417122 0.6417141 0.6417150 0.6417146
## [22] 0.6417143 0.6417145 0.6417144 0.6417144 0.6417144 0.6417144 0.6417144
## [29] 0.6417144 0.6417144 0.6417144 0.6417144 0.6417144 0.6417144
## 
## $precision
## [1] 5.820766e-11
## 
## $iteraciones
## [1] 34

La raíz es 0.6417144 Utiliando la función de pracma:

bisect(f,0,1,500)
## $root
## [1] 0.6417144
## 
## $f.root
## [1] -2.220446e-16
## 
## $iter
## [1] 54
## 
## $estim.prec
## [1] 1.110223e-16

Ejercicio 2

Usa el método de la bisección para encontrar una raíz con una precisión de \(10^{-2}\) para \(x^3-7x^2+14x-6=0\) en cada intervalo.

\[\begin{equation} a) [0,1]\qquad\qquad b) [1, 3.2]\qquad\qquad c)[3.2, 4] \end{equation}\]

f <- function(x){x^3-7*x^2+14*x-6}
grafica(f,-1,5,"Segunda gráfica")
bisect(f,0,1,500)
## $root
## [1] 0.5857864
## 
## $f.root
## [1] -8.881784e-16
## 
## $iter
## [1] 54
## 
## $estim.prec
## [1] 1.110223e-16

Entre 0 y 1, la raíz es de 0.5857864

bisect(f,1,3.2,500)
## $root
## [1] 3
## 
## $f.root
## [1] 7.105427e-15
## 
## $iter
## [1] 54
## 
## $estim.prec
## [1] 4.440892e-16

Entre 1 y 3.2, la raíz es de 3

bisect(f,3.2,4,500)
## $root
## [1] 3.414214
## 
## $f.root
## [1] -2.131628e-14
## 
## $iter
## [1] 52
## 
## $estim.prec
## [1] 4.440892e-16

Entre 3.2 y 4, la raíz es de 3.414214

Ejercicio 3

Usa el metodo de la bisección para encontrar las soluciones con una precisión de \(10^{-5}\) para los siguientes problemas.

  1. \(x-2^{-x}=0\) para \(0\leq x\leq 1\)
f <- function(x){x-2^(-x)}
grafica(f,-1,2,"Gráfica 3.a)")
bisect(f,0,1,500)
## $root
## [1] 0.6411857
## 
## $f.root
## [1] 0
## 
## $iter
## [1] 54
## 
## $estim.prec
## [1] 1.110223e-16

La raíz está en 0.6411857

  1. \(e^x-x^2+3x-2=0\) para \(0\leq x\leq 1\)
f <- function(x){exp(x)-x^2+3*x-2}

grafica(f,-1,1,"Gráfica 3.b)")
bisect(f,0,1,500)
## $root
## [1] 0.2575303
## 
## $f.root
## [1] -4.440892e-16
## 
## $iter
## [1] 55
## 
## $estim.prec
## [1] 5.551115e-17

La raíz está en 0.2575303

  1. \(2x\cos (2x)-(x+1)^2=0\) para \(-3\leq x\leq -2\) y \(-1\leq x \leq 0\)
f <- function(x){2*x*cos(2*x)-(x+1)^2}
grafica(f,-4,1,"Gráfica 3.c)")
bisect(f,-3,-2,500)
## $root
## [1] -2.191308
## 
## $f.root
## [1] -3.108624e-15
## 
## $iter
## [1] 52
## 
## $estim.prec
## [1] 4.440892e-16

La raíz entre -3 y -2 es de -2.191308

bisect(f,-1,0,500)
## $root
## [1] -0.79816
## 
## $f.root
## [1] 4.857226e-17
## 
## $iter
## [1] 54
## 
## $estim.prec
## [1] 1.110223e-16

La raíz entre -1 y 0 es de -0.79816

  1. \(x\cos x-2x^2+3x-1=0\) para \(0.2\leq x\leq 0.3\) y \(1.2\leq x \leq 1.3\)
f <- function(x){x*cos(x)-2*x^2+3*x-1}
grafica(f,0.1,1.4,"Gráfica 3.d)")
bisect(f,0.2,0.3,500)
## $root
## [1] 0.2975302
## 
## $f.root
## [1] 0
## 
## $iter
## [1] 52
## 
## $estim.prec
## [1] 5.551115e-17

La raíz entre 0.2 y 0.3 es de 0.2975302

bisect(f,1.2,1.3,500)
## $root
## [1] 1.256623
## 
## $f.root
## [1] 8.881784e-16
## 
## $iter
## [1] 49
## 
## $estim.prec
## [1] 2.220446e-16

La raíz entre 1.2 y 1.3 es de 1.256623

Ejercicio 4

Considera las funciones \(f(x)=x\) y \(g(x)=2 \sin x\). Usa el método de la bisección para encontrar una aproximación con una precisión de \(10^{-5}\) para el primer valor positivo \(x\) tal que \(f(x)=g(x)\).

g <- function(x){2*sin(x)}
h <- function(x){2*sin(x)-x}
graficas(g,-5,5,"Gráfica 4")
bisect(h,1,2.5,500)
## $root
## [1] 1.895494
## 
## $f.root
## [1] 0
## 
## $iter
## [1] 53
## 
## $estim.prec
## [1] 2.220446e-16

La raíz está en 1.895494 cuando f(x)=g(x) en su valor positivo

Ejercicio 5

Sea \(f(x)=(x+2)(x+1)x(x-1)^3(x-2)\). ¿A cuál raíz de \(f\) converge el método de la bisección cuando se aplica a los siguientes intervalos?

\[\begin{equation} a) [-3,2.5]\qquad \qquad b) [-2.5, 3]\qquad\qquad c)[-1.75, 1.5]\qquad\qquad d) [-1.5, 1.75] \end{equation}\]

Ejercicio 6

En cada una de las siguientes ecuaciones, determina un intervalo \([a,b]\) en que convergerá la iteración de punto fijo. Estima la cantidad de iteraciones necesarias para obtener aproximaciones con una exactitud de \(10^{-5}\) y realiza los cálculos.

it_pf <- function(g, q0, pr=1e-5, N=100){
  cond <- 1
  it <- 1
  q <- q0
  while(cond==1){
    if(it<=N){
      q[it+1] = g(q[it]) # iteración de la función 
      pr_it <- abs(q[it+1]-q[it]) # precisión en la iteración 
       if(pr_it<pr){
          resultados <- list(sucesion=q, precision=pr_it, iteraciones=it)
          return(resultados)
          cond <- 0
          }#final del segundo if
        else{it <- it+1}
    }#final del primer if
    else{
      print("Se alcanzo el maximo de iteraciones")
      cond <- 0
    }#fin del else
  }#final del while
}# final de la función
  1. \(\quad x=\frac{2-e^{x}+x^{2}}{3}\)
g <- function(x){(2-exp(x)+x^2)/3}
graficas(g,-2,2,"6.a)")
it_pf(g,0,pr=1e-10)
## $sucesion
##  [1] 0.0000000 0.3333333 0.2384996 0.2625130 0.2562399 0.2578654 0.2574433
##  [8] 0.2575529 0.2575244 0.2575318 0.2575299 0.2575304 0.2575303 0.2575303
## [15] 0.2575303 0.2575303 0.2575303 0.2575303 0.2575303
## 
## $precision
## [1] 3.945999e-11
## 
## $iteraciones
## [1] 18
bisect(h,0,1)
## $root
## [1] 3.054936e-151
## 
## $f.root
## [1] 3.054936e-151
## 
## $iter
## [1] 500
## 
## $estim.prec
## [1] 6.109873e-151
  1. \(\quad x=\frac{5}{x^{2}}+2\)

  2. \(\quad x=\left(e^{x} / 3\right)^{1 / 2}\)

f <- function(x){x}
g <- function(x){sqrt(exp(x)/3)}
h <- function(x){g(x)-f(x)}

graf <- ggplot()+
  #geom_vline(xintercept = 0, linetype="dashed")+ #eje x
  #geom_hline(yintercept = 0, linetype="dashed")+ #eje y
  #geom_line(aes(x=x_a, y=y_a), color="red", linewidth=1)+
  geom_function(fun=f,color="red",linewidth=0.75,n=200)+
  geom_function(fun=g,color="blue",linewidth=0.75,n=200)+
  geom_function(fun=h,color="yellow",linewidth=0.75,n=200)+
  coord_fixed(ratio = 1)+ # misma escala en los ejes
    xlim(-2,2)+
  labs(x="x", y="f(x)", title="Primera gráfica")+
  theme_bw()


ggplotly(graf)
it_pf(g,1,pr=1e-10)
## $sucesion
##  [1] 1.0000000 0.9518897 0.9292650 0.9188121 0.9140225 0.9118362 0.9108400
##  [8] 0.9103864 0.9101800 0.9100860 0.9100433 0.9100238 0.9100150 0.9100109
## [15] 0.9100091 0.9100083 0.9100079 0.9100077 0.9100076 0.9100076 0.9100076
## [22] 0.9100076 0.9100076 0.9100076 0.9100076 0.9100076 0.9100076 0.9100076
## 
## $precision
## [1] 6.564504e-11
## 
## $iteraciones
## [1] 27
bisect(h,0,2)
## $root
## [1] 0.9100076
## 
## $f.root
## [1] 0
## 
## $iter
## [1] 55
## 
## $estim.prec
## [1] 1.110223e-16
  1. \(\quad x=5^{-x}\)

  2. \(\quad x=6^{-x}\)

  3. \(\quad x=0.5(\sin x+\cos x)\)

f <- function(x){x}
g <- function(x){0.5*(sin(x)+cos(x))}
h <- function(x){g(x)-f(x)}

graf <- ggplot()+
  #geom_vline(xintercept = 0, linetype="dashed")+ #eje x
  #geom_hline(yintercept = 0, linetype="dashed")+ #eje y
  #geom_line(aes(x=x_a, y=y_a), color="red", linewidth=1)+
  geom_function(fun=f,color="red",linewidth=0.75,n=200)+
  geom_function(fun=g,color="blue",linewidth=0.75,n=200)+
  geom_function(fun=h,color="yellow",linewidth=0.75,n=200)+
  coord_fixed(ratio = 1)+ # misma escala en los ejes
    xlim(-2,2)+
  labs(x="x", y="f(x)", title="Primera gráfica")+
  theme_bw()


ggplotly(graf)
it_pf(g,0.75,pr=1e-10)
## $sucesion
## [1] 0.7500000 0.7066638 0.7049162 0.7048179 0.7048123 0.7048120 0.7048120
## [8] 0.7048120 0.7048120
## 
## $precision
## [1] 5.868017e-11
## 
## $iteraciones
## [1] 8
bisect(h,0,1)
## $root
## [1] 0.704812
## 
## $f.root
## [1] 1.110223e-16
## 
## $iter
## [1] 54
## 
## $estim.prec
## [1] 1.110223e-16